
package com.android.matmanual;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class LEDTest extends Activity implements OnClickListener{
	Button returnBt;
	SeekBar ledSeekbar;
	TextView ledTv;
	WakeLock mWakeLock;
	WindowManager.LayoutParams mLayoutParams;
	int mProgress =0;
    private static final int MAXIMUM_BACKLIGHT = 9;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitle(getResources().getString(R.string.checkledtitle));
		setContentView(R.layout.led_test);
		 PowerManager pm =
		           (PowerManager) getSystemService(POWER_SERVICE); 
		    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, 
		          "XYTEST"); 
		    mWakeLock.acquire(); 

		mLayoutParams = getWindow().getAttributes();
		//closeBt=(Button)findViewById(R.id.closebt);
		returnBt=(Button)findViewById(R.id.returnbt);
		ledTv=(TextView)findViewById(R.id.ledtv);
		ledSeekbar=(SeekBar)findViewById(R.id.ledseekbar);
		ledSeekbar.setMax(MAXIMUM_BACKLIGHT-1);
		ledSeekbar.setProgress(mProgress);
		//closeBt.setOnClickListener(this);
		returnBt.setOnClickListener(this);
		ledSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				// TODO Auto-generated method stub
				setBrightness(arg1+1);
				ledHandler.sendEmptyMessage(arg1+1);
			}
		});
	}
	
    @Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setBrightness(1);
		ledHandler.sendEmptyMessage(1);
		ledSeekbar.setProgress(mProgress);
	}

	public void setBrightness(int brightness) {
    	mLayoutParams.screenBrightness = (float)brightness/MAXIMUM_BACKLIGHT;
        getWindow().setAttributes(mLayoutParams);
    }
	public void onClick(View view) {
		// TODO Auto-generated method stub
		int id=view.getId();
		switch (id) {
        case R.id.returnbt:
			finish();
			break;
		default:
			break;
		}
	}
	Handler ledHandler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			ledTv.setText(getResources().getString(R.string.lightinduction)+(msg.what));
		}
	};
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
        finish();
		super.onStop();
	}	
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(mWakeLock!=null)
		{
			mWakeLock.release(); 
			mWakeLock = null;
		}
	}	
	@Override
	public void onAttachedToWindow() 
	{
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
		this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		int result = TestControl.GetKeyEvent(keyCode);
		if (result != TestControl.DO_NOTHING) {
			setResult(result);
			finish();
	    }
	    return true;
	}
}
		 